package com.sgb.kjwl.view.widget.enterprise;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.enterprise.ServiceProjectClassEntity;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceProSkuAdapter;
import com.sgb.kjwl.view.widget.MaxHeightRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;

import org.apache.commons.collections4.ListUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ServiceProSkuPopWindow extends BasePopWindow {

    private static final String NO_NAME = "type_no_";
    private static final String NAME = "type_name_";
    private static final String PRICE = "price";
    private MaxHeightRecyclerView recyclerView;
    private ImageView closeView;
    private TextView tvPrice;
    private TextView tv_close;
    private ServiceProSkuAdapter mAdapter;
    private List<ServiceProjectClassEntity> mData;
    private List<Map> mMaps;
    private List<Long> firstSelectId = new ArrayList<>();
    private List<List<Long>> skuList = new ArrayList<>();
    private List<String> skuPrice = new ArrayList<>();
    private List<String> skuName = new ArrayList<>();
    private LinearLayout noDataLay;
    private IPrice iPrice;
    private boolean isFirst = true;
    private boolean isOk = false;

    public ServiceProSkuPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_service_pro_sku;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.node_list);
        tv_close = holder.getPop().findViewById(R.id.tv_close);
        tvPrice = holder.getPop().findViewById(R.id.tv_price);
        closeView = holder.getPop().findViewById(R.id.close_view);
        noDataLay = holder.getPop().findViewById(R.id.ll_empty);
        closeView.setOnClickListener(v -> dissMiss());
        mAdapter = new ServiceProSkuAdapter(mContext, null);
        mAdapter.setItemSelect(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
        mAdapter.onDefaultData(new ServiceProSkuAdapter.IDefaultData() {
            @Override
            public void getDefaultData(List<Long> numberList) {
//                isOk = false;
                getSkuPrice(mAdapter.getNumberList());

            }

            @Override
            public void getSelectName(List<String> nameList) {
                skuName = nameList;
                String selectName = "";
                iPrice.setSelectName(skuName.toString());
                for (int i = 0; i < skuName.size(); i++) {
                    selectName = selectName + skuName.get(i) + " | ";
                }
                iPrice.setSelectName(selectName.substring(0, selectName.length() - 2));
            }
        });
        tv_close.setOnClickListener(v -> {
//            isOk = true;

            dismiss();
        });
    }

    public void setData(List<ServiceProjectClassEntity> data, List<Map> maps) {
        mData = data;
        mMaps = maps;
        if (mData == null || mData.size() == 0) {
            mAdapter.setDatasMayNull(null);
            noDataLay.setVisibility(View.VISIBLE);
            tvPrice.setVisibility(View.INVISIBLE);
        } else {
            mAdapter.setDatas(data);
        }
        if (mMaps != null)
            checkArr();
        getSelectName();
        isOk = true;
        getSkuPrice(firstSelectId);
    }

    public void checkArr() {
        for (int i = 0; i < mMaps.size(); i++) {
            String key;
            Long value;
            Iterator it = mMaps.get(i).keySet().iterator();
            List<Long> dataList = new ArrayList<>();
            while (it.hasNext()) {
                key = (String) it.next();
                if (key.equals(NO_NAME + 1)) {
                    value = new Double((Double) mMaps.get(i).get(key)).longValue();
                    Log.e(key, ":" + value);
                    if (value != 0)
                        dataList.add(value);
                } else if (key.equals(NO_NAME + 2)) {
                    value = new Double((Double) mMaps.get(i).get(key)).longValue();
                    Log.e(key, ":" + value);
                    if (value != 0)
                        dataList.add(value);
                } else if (key.equals(NO_NAME + 3)) {
                    value = new Double((Double) mMaps.get(i).get(key)).longValue();
                    Log.e(key, ":" + value);
                    if (value != 0)
                        dataList.add(value);
                } else if (key.equals(NO_NAME + 4)) {
                    value = new Double((Double) mMaps.get(i).get(key)).longValue();
                    Log.e(key, ":" + value);
                    if (value != 0)
                        dataList.add(value);
                } else if (key.equals(NO_NAME + 5)) {
                    value = new Double((Double) mMaps.get(i).get(key)).longValue();
                    Log.e(key, ":" + value);
                    if (value != 0)
                        dataList.add(value);
                }
                if (key.equals(PRICE)) {
                    skuPrice.add(String.valueOf(mMaps.get(i).get(key)));
                }
            }
            skuList.add(dataList);
        }
    }

    private void getSkuPrice(List<Long> selectData) {
        for (int a = 0; a < skuList.size(); a++) {
            if (isListEqual(selectData, skuList.get(a))) {
                Log.e("相同下标", ":" + a);
                Double dP = Double.valueOf(skuPrice.get(a));
                tvPrice.setText(dP == -1.0 ? "议价" : "￥" + PointUtils.price(dP));
//                if (isOk == true) {
                    iPrice.getSelectPrice(dP == -1.0 ? "议价" : skuPrice.get(a));
//                }
            }
        }
    }

    private void getSelectName() {
        String selectName = "";
        if (isFirst) {
            isFirst = false;
            for (int i = 0; i < mData.size(); i++) {
                selectName = selectName + mData.get(i).getChild().get(0).getName() + "|";
                firstSelectId.add(mData.get(i).getChild().get(0).getNo());
            }
        }
        iPrice.setSelectName(selectName.substring(0, selectName.length() - 1));
    }

    public interface IPrice {
        void getSelectPrice(String price);

        void setSelectName(String name);
    }

    public void onPrice(IPrice iPrice) {
        this.iPrice = iPrice;
    }

    public static <E> boolean isListEqual(List<E> list1, List<E> list2) {
        // 两个list引用相同（包括两者都为空指针的情况）
        if (list1 == list2) {
            return true;
        }

        // 两个list都为空（包括空指针、元素个数为0）
        if ((list1 == null && list2 != null && list2.size() == 0)
                || (list2 == null && list1 != null && list1.size() == 0)) {
            return true;
        }

        // 两个list元素个数不相同
        if (list1.size() != list2.size()) {
            return false;
        }

        // 两个list元素个数已经相同，再比较两者内容
        // 采用这种可以忽略list中的元素的顺序
        // 涉及到对象的比较是否相同时，确保实现了equals()方法
        if (!list1.containsAll(list2)) {
            return false;
        }

        return true;
    }
}
